<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

class Index extends Controller
{
    public function index()
    {
        $character=Db::name('character')
            ->where('ch_state',1)
            ->where('ch_click','>',100)
            ->limit(20)
            ->order('ch_click','desc')
            ->select();

        $group=Db::name('group')
            ->where('gr_state',1)
            ->where('gr_click','>',100)
            ->limit(20)
            ->select();
        $chrew=Db::name('reward')
            ->alias('a')
            ->join('character b','a.re_chid = b.ch_id')
            ->order('re_rew','desc')
            ->limit(20)
            ->select();
        //dump($chrew);
        $this->assign('character',$character);
        $this->assign('group',$group);
        $this->assign('chrew',$chrew);
        return $this->fetch('index');
    }

    public function chlist()
    {
        $chrew=Db::name('reward')
            ->alias('a')
            ->join('character b','a.re_chid = b.ch_id')
            ->order('re_rew','desc')
            ->limit(20)
            ->select();

        $character=Db::name('character')
            ->where('ch_state',1)
            ->paginate(5);

        $page = $character->render();

        $this->assign('chrew',$chrew);
        $this->assign('character',$character);
        $this->assign('page', $page);

        return $this->fetch('index/list');
    }

    public function glist()
    {
        $chrew=Db::name('reward')
            ->alias('a')
            ->join('character b','a.re_chid = b.ch_id')
            ->order('re_rew','desc')
            ->limit(20)
            ->select();

        $group=Db::name('group')
            ->where('gr_state',1)
            ->select();

        $this->assign('chrew',$chrew);
        $this->assign('group',$group);
        return $this->fetch('index/glist');
    }

    public function s_list()
    {
        $keys=$_POST['keywords'];
        $chrew=Db::name('reward')
            ->alias('a')
            ->join('character b','a.re_chid = b.ch_id')
            ->order('re_rew','desc')
            ->limit(20)
            ->select();

        $character=Db::name('character')
            ->where('ch_state',1)
            ->where('ch_name','like','%'.$keys.'%')
            ->select();

        $this->assign('chrew',$chrew);
        $this->assign('character',$character);
        $this->assign('keys',$keys);
        return $this->fetch('index/s_list');
    }

    public function view()
    {
        $chid=$_GET['chid'];
        $chrew=Db::name('reward')
            ->alias('a')
            ->join('character b','a.re_chid = b.ch_id')
            ->order('re_rew','desc')
            ->limit(20)
            ->select();

        $character=Db::name('character')
            ->where('ch_id',$chid)
            ->alias('a')
            ->join('group b','a.ch_gid = b.gr_id')
            ->join('reward c','a.ch_id = c.re_chid')
            ->find();

        Db::name('character')
            ->where('ch_id', $chid)
            ->setInc('ch_click');


        //dump($character);
        $this->assign('chrew',$chrew);
        $this->assign('character',$character);
        return $this->fetch('index/view');
    }
}
